﻿using System;
using System.Windows.Forms;
using Noogen.Validation;

namespace FinanceManager
{
    public partial class AddWorker : Form
    {
        public AddWorker()
        {
            InitializeComponent();
        }

        private void btnAddworker_Click(object sender, EventArgs e)
        {
            ValidationRule vr1 = new ValidationRule();
            vr1.IsRequired = true;
            this.validationProvider.SetValidationRule(tbwname,vr1);

            ValidationRule vr2 = new ValidationRule();
            //vr2.ErrorMessage = "%ControlName% must be 7 characters long.";
            vr2.RegExPattern = "^\\d{11}$";
            this.validationProvider.SetValidationRule(tbwphone,vr2);

            ValidationRule vr3 = new ValidationRule();
            vr3.DataType = Noogen.Validation.ValidationDataType.Double;
            //vr3.ErrorMessage = "%ControlName% is not between 1 and 10.";
            vr3.MaximumValue = "300";
            vr3.MinimumValue = "1";
            this.validationProvider.SetValidationRule(tbwwage,vr3);

            this.validationProvider.ValidationMessages(!this.validationProvider.Validate());
            if(validationProvider.Validate()){
                if (workerTableAdapter_add.InsertWorker(tbwname.Text.ToString(), Decimal.Parse(tbwphone.Text), long.Parse(tbwwage.Text))==1) {
                    MessageBox.Show("添加成功!");
                    InitControl();
                }
            }
        }

        public void InitControl() {
            tbwname.Text = "";
            tbwphone.Text = "";
            tbwwage.Text = "";
        }

        private void AddWorker_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
